package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.collector.Collector;
import org.top10.entity.Blog;
import org.top10.entity.CollectEntity;
import org.top10.entity.Emotion;
import org.top10.entity.Ent;
import org.top10.entity.Finance;
import org.top10.entity.Forum;
import org.top10.entity.News;
import org.top10.entity.Reading;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class Net163Task extends Task{

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_Net163;// 对应网站ID
	private static final String ENCoding = "gb2312";// 对应网站ID

	public Net163Task() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new Net163EntertainmentParser());
		PARSER.add(new Net163VNewsParser());
		PARSER.add(new NetViewParser());
		PARSER.add(new Net163NewsClickParser());
		PARSER.add(new Net163NewsReplyParser());
		PARSER.add(new Net163EntClickParser());
		PARSER.add(new Net163EntReplyParser());
		PARSER.add(new Net163SportsClickParser());
		PARSER.add(new Net163SportsReplyParser());
		PARSER.add(new Net163FinanceClickParser());
		PARSER.add(new Net163FinanceReplyParser());
		PARSER.add(new Net163EmotionClickParser());
		PARSER.add(new Net163EmotionReplyParser());
		PARSER.add(new Net163ReadingClickParser());
		PARSER.add(new Net163ReadingReplyParser());
		PARSER.add(new Net163BlogParser());
		PARSER.add(new Net163ForumClickParser());
		PARSER.add(new Net163ForumReplyParser());
	}

	public static void main(String args[]) throws Exception {
		new Net163ForumReplyParser().dealText(ENCoding);
	}
}

abstract class Net163Parser extends TOPParserImpl {

	public Net163Parser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;
	public abstract CollectEntity build(Node node, int rank) throws Exception;
	
	protected List parseNet163(Node node)throws Exception{
		
		if(node!=null){
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			while (rank<11) {
				CollectEntity v = build(dls.get(rank-1), rank++);
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
	protected List parseNet163(ParseSource doc,String sign)throws Exception{
		
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"h2",sign);
		if(node!=null){
			node=node.getParentNode().getNextSibling().getNextSibling();
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"a",as);
			for(int i=0;i<10;i++){
				CollectEntity v = build(as.get(i),i+1);
				System.out.println(v.toString());
				entitys.add(v);
			}
			return entitys;
		}
		
		return null;
	}
	protected  Video buildVideo(Node node, int rank){
		Video v = new Video();
		v.setRank(rank);
		String url = getAttrValueByTag(node, "href");
		v.setPlayUrl(url);
		v.setName(getAttrValueByTag(node, "title"));
		return v;
	}
	protected  void buildAll(Node node, int rank, CollectEntity v){
		v.setRank(rank);
		String url = getAttrValueByTag(node, "href");
		v.setLinkUrl(url);
		v.setName(node.getTextContent());
	}
	
	protected  List buildForumList(ParseSource doc,String sign) throws Exception{
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"h2",sign);
		if(node!=null){
			node=node.getParentNode().getNextSibling().getNextSibling();
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"a",as);
			for(int i=0;i<10;i++){
				CollectEntity v = build(as.get(i),i+1);
				System.out.println(v.toString());
				entitys.add(v);
			}
			return entitys;
		}
		
		return null;
	}
}
class Net163ForumClickParser extends Net163Parser {
	private static final String SIGN = "24小时热帖排行";
	private static final String URL="http://bbs.163.com/";
	private static final String TITLE=SIGN;
	public Net163ForumClickParser() {
		super(TITLE, TOP10Global.TYPE_Forum,URL);
	}
	public Forum build(Node node, int rank) throws Exception {
		Forum n=new Forum();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  buildForumList(doc,SIGN);
	}
}
class Net163ForumReplyParser extends Net163Parser {
	private static final String SIGN = "24小时回帖排行";
	private static final String URL="http://bbs.163.com/";
	private static final String TITLE=SIGN;
	public Net163ForumReplyParser() {
		super(TITLE, TOP10Global.TYPE_Forum,URL);
	}
	public Forum build(Node node, int rank) throws Exception {
		Forum n=new Forum();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  buildForumList(doc,SIGN);
	}
}
class Net163ReadingClickParser extends Net163Parser {
	private static final String SIGN = "点击榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_book.html";
	private static final String TITLE="读书点击榜";
	public Net163ReadingClickParser() {
		super(TITLE, TOP10Global.TYPE_Reading,URL);
	}
	public Reading build(Node node, int rank) throws Exception {
		Reading n=new Reading();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163BlogParser extends Net163Parser {
	private static final String SIGN = "周排行榜";
	private static final String URL="http://blog.163.com/";
	private static final String TITLE="周排行榜";
	public Net163BlogParser() {
		super(TITLE, TOP10Global.TYPE_Blog,URL);
	}
	public Blog build(Node node, int rank) throws Exception {
		Blog b=new Blog();
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node,"a",as);
		node=as.get(0);
		b.setRank(rank);
		b.setLinkUrl(getAttrValueByTag(node, "href"));
		b.setName(node.getTextContent());
		return b;
	}
	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"span",SIGN);
		if(node!=null){
			node=node.getParentNode().getParentNode();
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"li",as);
			for(int i=1;i<11;i++){
				CollectEntity v = build(as.get(i),i+1);
				System.out.println(v.toString());
				entitys.add(v);
			}
			return entitys;
		}
		return null;
	}
}


class Net163ReadingReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_book.html";
	private static final String TITLE="读书跟贴榜";
	public Net163ReadingReplyParser() {
		super(TITLE, TOP10Global.TYPE_Reading,URL);
	}
	public Reading build(Node node, int rank) throws Exception {
		Reading n=new Reading();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}

class Net163EmotionClickParser extends Net163Parser {
	private static final String SIGN = "点击榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_lady.html";
	private static final String TITLE="情感点击榜";
	public Net163EmotionClickParser() {
		super(TITLE, TOP10Global.TYPE_Emotion,URL);
	}
	public Emotion build(Node node, int rank) throws Exception {
		Emotion n=new Emotion();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163EmotionReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_lady.html";
	private static final String TITLE="情感跟贴榜";
	public Net163EmotionReplyParser() {
		super(TITLE, TOP10Global.TYPE_Emotion,URL);
	}
	public Emotion build(Node node, int rank) throws Exception {
		Emotion n=new Emotion();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163FinanceClickParser extends Net163Parser {
	private static final String SIGN = "点击榜";
	private static final String URL="http://money.163.com/special/002526BH/rank.html";
	private static final String TITLE="财经点击榜";
	public Net163FinanceClickParser() {
		super(TITLE, TOP10Global.TYPE_Finance,URL);
	}
	public Finance build(Node node, int rank) throws Exception {
		Finance n=new Finance();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163FinanceReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://money.163.com/special/002526BH/rank.html";
	private static final String TITLE="财经跟贴榜";
	public Net163FinanceReplyParser() {
		super(TITLE, TOP10Global.TYPE_Finance,URL);
	}
	public Finance build(Node node, int rank) throws Exception {
		Finance n=new Finance();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163NewsClickParser extends Net163Parser {
	private static final String SIGN = "点击榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_news.html";
	private static final String TITLE="新闻点击榜";
	public Net163NewsClickParser() {
		super(TITLE, TOP10Global.TYPE_News,URL);
	}
	public News build(Node node, int rank) throws Exception {
		News n=new News();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163NewsReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_news.html";
	private static final String TITLE="新闻跟贴榜";
	public Net163NewsReplyParser() {
		super(TITLE, TOP10Global.TYPE_News,URL);
	}
	public News build(Node node, int rank) throws Exception {
		News n=new News();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163EntClickParser extends Net163Parser {
	private static final String SIGN = "点击榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_ent.html";
	private static final String TITLE="娱乐点击榜";
	public Net163EntClickParser() {
		super(TITLE, TOP10Global.TYPE_ENT,URL);
	}
	public Ent build(Node node, int rank) throws Exception {
		Ent n=new Ent();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163EntReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_ent.html";
	private static final String TITLE="娱乐跟贴榜";
	public Net163EntReplyParser() {
		super(TITLE, TOP10Global.TYPE_ENT,URL);
	}
	public Ent build(Node node, int rank) throws Exception {
		Ent n=new Ent();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
//////////////--------------------------------------------
class Net163SportsReplyParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_ent.html";
	private static final String TITLE="娱乐跟贴榜";
	public Net163SportsReplyParser() {
		super(TITLE, TOP10Global.TYPE_Sports,URL);
	}
	public Ent build(Node node, int rank) throws Exception {
		Ent n=new Ent();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163SportsClickParser extends Net163Parser {
	private static final String SIGN = "跟贴榜";
	private static final String URL="http://news.163.com/special/0001386F/rank_ent.html";
	private static final String TITLE="娱乐跟贴榜";
	public Net163SportsClickParser() {
		super(TITLE, TOP10Global.TYPE_Sports,URL);
	}
	public Ent build(Node node, int rank) throws Exception {
		Ent n=new Ent();
		buildAll(node,rank,n);
		return n;
	}
	public List parse(ParseSource doc) throws Exception {
		return  parseNet163(doc,SIGN);
	}
}
class Net163EntertainmentParser extends Net163Parser {

	private static final String SIGN = "list01";
	private static final String URL="http://v.163.com/ent/";
	private static final String TITLE="日排行";
	public Net163EntertainmentParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("ul", "id",SIGN, body);
		return  parseNet163(node);
	}
}
class Net163VNewsParser extends Net163Parser {

	private static final String SIGN = "list01";
	private static final String URL="http://v.163.com/news/";
	private static final String TITLE="日排行";
	public Net163VNewsParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("ul", "id",SIGN, body);
		return  parseNet163(node);
	}
}
class NetViewParser extends Net163Parser {

	private static final String SIGN = "list01";
	private static final String URL="http://v.163.com/nv/";
	private static final String TITLE="日排行";
	public NetViewParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("ul", "id",SIGN, body);
		return  parseNet163(node);
	}
}
	// XUNlei所有解析器的公有方法放在这里..
	
